package cn.getech.data.metadata.mapper;

import cn.getech.data.metadata.model.res.collectionTableAnalysis.CollectionTableAnalysisRes;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface CollectionTableAnalysisMapper {

    @Select("SELECT\n" +
            "table_info_id as tableInfoId," +
            "id as tableId," +
            "table_type as tableType," +
            "\t'当前表' AS type,\n" +
            "\tCONCAT(db_name, '.', table_name) AS `tableName`,\n" +
            "\t  CASE\n" +
            "    WHEN cti.table_type = 1 THEN\n" +
            "    (select IF(store_engine=1,'Hive','Kudo') as type from  bdp_table_info where id=cti.table_info_id limit 1 )\n" +
            "    WHEN cti.table_type = 2 THEN 'Kafka'\n" +
            "    WHEN cti.table_type = 3 AND cti.source_id > 0  THEN\n" +
            "    (select\n" +
            "    CASE\n" +
            "    WHEN type_id = 1 THEN 'MySQL'\n" +
            "    WHEN type_id = 2 THEN 'Oracle'\n" +
            "    WHEN type_id = 3 THEN 'SQL Server'\n" +
            "    WHEN type_id = 4 THEN 'PostgreSQL'\n" +
            "    WHEN type_id = 11 THEN 'Hive'\n" +
            "    ELSE '其他' end  as   dbType\n" +
            "    from bdp_conf_connect where id=cti.source_id  limit 1\n" +
            "    )\n" +
            "    ELSE '其他'\n" +
            "    END AS dbType,\n" +
            "\tdb_name AS dbName,\n" +
            "\t(SELECT username FROM sys_user WHERE user_id = cti.create_user) as createUserName,\n" +
            "\t(CASE WHEN table_type = 3 THEN view_count ELSE ((SELECT view_num from bdp_table_info bti WHERE bti.id = cti.table_info_id)\n" +
            "\t\t+(SELECT count(*) FROM bdp_proc_table p WHERE p.table_id = cti.table_info_id)\n" +
            "\t) END ) AS heatNum\n" +
            "FROM\n" +
            "\tcollection_table_info cti \n" +
            "WHERE\n" +
            "\tcti.id = #{collectionId}")
    CollectionTableAnalysisRes getMetadataTable(Integer collectionId);

    List<CollectionTableAnalysisRes> getMetadataTableList(@Param("params") Map<String, Object> params, @Param("collectionId")Integer collectionId, @Param("tableInfoId")Integer tableInfoId, @Param("flag")String flag);
}
